#include "customizeedit.h"
#include <QDebug>

CustomizeEdit::CustomizeEdit(QWidget* parent) : QLineEdit(parent), m_max_len(0)
{
    connect(this, &QLineEdit::textChanged, this, &CustomizeEdit::limitTextLength);
}

void CustomizeEdit::SetMaxLength(int max_len)
{
    m_max_len = max_len;
}

void CustomizeEdit::focusOutEvent(QFocusEvent *event)
{
    // 执行失去焦点时的处理逻辑
    qDebug() << "CustomizeEdit focusout";
    // 调用基类的focusOutEvent()方法，保证基类的行为得到执行
    QLineEdit::focusOutEvent(event);
    //发送失去焦点得信号
    emit sig_foucus_out();
}

void CustomizeEdit::limitTextLength(QString text)
{
    if (m_max_len <= 0) {
        return ;
    }

    QByteArray bytearray = text.toUtf8();
    if (bytearray.size() > m_max_len) {
        bytearray = bytearray.left(m_max_len);
        this->setText(QString::fromUtf8(bytearray));
    }
}
